package tgc.edu.ljc.web.controller;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.ljc.custom.JqGridParam;
import tgc.edu.ljc.custom.JqGridUtils;
import tgc.edu.ljc.entity.Stu;
import tgc.edu.ljc.entity.TbClass;
import tgc.edu.ljc.repository.RoleRepository;
import tgc.edu.ljc.service.ClassService;
import tgc.edu.ljc.service.StudentService;
import tgc.edu.ljc.web.form.StudentForm;

@Controller
@RequestMapping(value="/student")
public class StudentController {
	@Autowired
	private StudentService service;
	@Autowired
	private ClassService classService;
	@Autowired
	private RoleRepository roleDAO;
	
	@RequestMapping(value="/manage")
	public String manage(ModelMap map) {
		List<TbClass> classes = classService.findAll();
		Integer classId=0;
		if(!classes.isEmpty()) {
			classId=classes.get(0).getId();
		}
		map.put("classes", classes);
		map.put("classId", classId);
		return "student/manage";
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(JqGridParam param,Integer classId) {
		
		Pageable pageable = param.buildPageable();
		Specification spec=new Specification<Stu>() {

			@Override
			public Predicate toPredicate(Root<Stu> root, CriteriaQuery<?> arg1, CriteriaBuilder cb) {
				Predicate predicate = cb.equal(root.get("tbClass").get("id"), classId);
				return predicate;
			}
		};
		Page<Stu> page;
		if(param.getSearch()) {
			spec =spec.and(param.buildSpec());
		}
		page=service.findAll(spec, pageable);
		List<Stu> students = page.getContent();
		for (Stu stu : students) {
			stu.setPassword("");
		}
		return JqGridUtils.getResult(page);
	}
	
	@RequestMapping(value="/edit")
	@ResponseBody
	public Object edit(StudentForm form) {
		String oper = form.getOper();
		Stu model=new Stu();
		if("del".equals(oper)) {
			service.deleteById(form.getId());
		}else {
			if("edit".equals(oper)) {
				model=service.findById(form.getId());
			}else {
				model.getRoles().add(roleDAO.findById(2).get());
				if(!StringUtils.hasText(form.getPassword())) {
					form.setPassword("1111");
				}
			}
			BeanUtils.copyProperties(form, model,"id","password");
			if(StringUtils.hasText(form.getPassword())) {
				model.setPassword(new BCryptPasswordEncoder().encode(form.getPassword()));
			}
			service.save(model);
		}
		return "OK";
	}
	
	@RequestMapping(value="/findStuByClassId")
	@ResponseBody
	public Object findStuByClassId(Integer classId) {
		List<Stu> list=service.findByTbClassId(classId);
		return list;
	}
}
